package com.zlsoft.web.popup;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.util.KeyboardUtils;
import com.lxj.xpopup.util.XPopupUtils;
import com.zlsoft.web.base.BaseActivity;
import com.zlsoft.mobilenurse.R;
import com.zlsoft.web.utils.AppURLs;

/**
 * @author - qqz
 * @date - 2021-12-14/15:48
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @note -
 */
public class FirstInitPopup extends CenterPopupView {

    private EditText mEtPopupInput;

    private Button etPopupBtn;

    private CheckBox cbInlayUrl;

    private Context mContext;

    private boolean mFirstUse;
    // https://plugins.jetbrains.com/plugin/8261-findviewbyme

    public FirstInitPopup(@NonNull Context context, boolean firstUse) {
        super(context);
        this.mContext = context;
        this.mFirstUse=firstUse;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_input_url;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
        initListener();
    }

    @Override
    protected int getMaxWidth() {
        return (int) (XPopupUtils.getAppWidth(getContext()));
    }

    /**
     * 初始化控件
     */
    private void initView() {
        TextView tvTitle=findViewById(R.id.tv_popup_title);
        if (mFirstUse){
            tvTitle.setText("首次使用,需配置访问地址");
        }else {
            tvTitle.setText("配置有误,请更换正确的访问地址");
        }


        mEtPopupInput = (EditText) findViewById(R.id.et_popup_input);
        cbInlayUrl = (CheckBox) findViewById(R.id.cb_inlay_url);
        etPopupBtn = (Button) findViewById(R.id.et_popup_btn);
        findViewById(R.id.iv_close_dialog_click).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "退出应用", Toast.LENGTH_SHORT).show();
                System.exit(0);
            }
        });

        // 使用内置地址
        cbInlayUrl.setOnCheckedChangeListener((buttonView, isChecked) -> {
            mEtPopupInput.setText(isChecked ? AppURLs.getWebUrl() : "");
            if (isChecked) {
                Toast.makeText(mContext, "已使用内置 [" + ((BaseActivity) mContext).getTitle() + "] 路径", Toast.LENGTH_LONG).show();
            }
        });


    }

    /**
     * 初始化监听
     */
    private void initListener() {
        etPopupBtn.setOnClickListener(v -> {
            String mS = mEtPopupInput.getText().toString();
            if (TextUtils.isEmpty(mS)) {
                Toast.makeText(getContext(), "不允许为空", Toast.LENGTH_SHORT).show();
                return;
            }
            KeyboardUtils.hideSoftInput(getRootView());
            sSiteUrl = getAgreement(mS) + mS;
            dismiss();
        });
    }


    public static String sSiteUrl = "";


    private String getAgreement(String mS) {
        if (mS.contains("http")) {
            return "";
        }
        return "http://";
    }
}
